import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * 编译的最小单位是文件——
 *  确保类名和文件名之间有直接映射关系。
 *  方便在大型项目中快速找到对应的文件。 这是一个"约定俗成"
 *
 *  jvm只要能加载类就可以运行,不管是不是public「这个类启动-编译-运行」都是确切的文件路径,也即类路径确切
 * **/

class CompleteFuture {
    public static void main(String[] args) {
        List<Integer> data = BatchProcessing.generateData(10000);
        List<List<Integer>> batches = BatchProcessing.splitData(data, 100);

        List<CompletableFuture<Void>> futures = new ArrayList<>();
        for (List<Integer> batch : batches) {
            futures.add(CompletableFuture.runAsync(() -> BatchProcessing.processBatch(batch)));
        }

        CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join(); // 等待所有任务完成
        System.out.println("All batches processed!");
    }
}

class CountDownLatch {
    public static void main(String[] args) throws InterruptedException {
        List<Integer> data = BatchProcessing.generateData(10000);
        List<List<Integer>> batches = BatchProcessing.splitData(data, 100);
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        java.util.concurrent.CountDownLatch latch = new java.util.concurrent.CountDownLatch(batches.size());

        for (List<Integer> batch : batches) {
            executorService.submit(() -> {
                try {
                    BatchProcessing.processBatch(batch);
                } finally {
                    latch.countDown();
                }
            });
        }
        latch.await(); // 等待所有批次处理完成
        executorService.shutdown();
        System.out.println("All batches processed!");
    }

}
class ForkJoin {
    public static void main(String[] args) {
        List<Integer> data = BatchProcessing.generateData(10000);
        List<List<Integer>> batches = BatchProcessing.splitData(data, 100);

        ForkJoinPool pool = new ForkJoinPool(10); // 设置并行线程数
        pool.submit(() -> batches.parallelStream().forEach(BatchProcessing::processBatch)).join();
        pool.shutdown();

        System.out.println("All batches processed!");
    }
}

class BatchProcessing {
    /**
     * generateData(int size)：生成一个包含 size 个整数的数据列表。
     * splitData(List<Integer> data, int batchSize)：将数据按批次大小拆分成小的子列表（如 100 条数据为一批）。
     * processBatch(List<Integer> batch)：对某一批数据执行处理逻辑（这里是简单的打印）
     *
     * **/
    public static List<Integer> generateData(int size) {
        List<Integer> data = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            data.add(i);
        }
        return data;
    }
    public static List<List<Integer>> splitData(List<Integer> data, int batchSize) {
        List<List<Integer>> batches = new ArrayList<>();
        for (int i = 0; i < data.size(); i += batchSize) {
            batches.add(data.subList(i, Math.min(i + batchSize, data.size())));
        }
        return batches;
    }
    public static void processBatch(List<Integer> batch) {
        System.out.println("Processing batch: " + batch);
    }
}


